// pages/index/sureYuding.js
const App = getApp();
const navigationBarHeight = (App.statusBarHeight + 44) + 'px'
var config = require('../../utils/config');
var net = require('../../utils/net.js');
var util = require('../../utils/util.js');
import { String } from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navigationBarTitle: '予約情報',
    navigationBarHeight,


    noTedian: true,
    haveSelect: false,
    peopleCount: '2',
    peopleCountStr: '2人',
    peopleCountDatas: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
    date: '2018-03-01',
    startDate: '2018-03-01',
    endDate: '2020-01-01',
    dateStr: '2018年03月01日',
    time: '19:00',
    startTime:'00:00',
    tedian: 1,
    showPeopleCount: false,
    showInputBox:false,
    detailModel: {},
    orderModel: {},//从预定来
    selectItem: {},
    name: '',
    phone: '',
    beizhu:'',
    cc:'',

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new App.ToastPannel();
    this.from = options.from;
    this.personCount = -1;
    App.globalData.selectIndex = {}

    App.globalData.orderUserName = ''
    App.globalData.orderUserPhone = ''

    this.initStartTime()//初始化
    this.requestUserInfo()

    if (this.from == 1) {
      this.orderId = options.orderId;
      this.shopId = options.shopId;
      this.requestOrderInfo(); 
      return;
    }
   
    var dateStr = this.currentDay
    var dateArr = this.currentDay.split('-');
    if (dateArr.length > 0) {
      dateStr = dateArr[0] + '年' + dateArr[1] + '月' + dateArr[2] + '日';
    }

    console.log('设置当前时间', this.currentTime)
    this.setData({
      date: this.currentDay,
      dateStr: dateStr,
      time:this.currentTime
    })
    this.orderId = '空';
    this.shopId = options.shopId;
    this.requestShopInfo()//请求商店信息
    
  },



  requestShopInfo: function () {
    var url = config.service.businessDetailsUrl;
    var params = {
      merchantId: this.shopId,
      latitude: App.globalData.lat,
      longitude: App.globalData.lon
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      if (res.code == 0) {
        var model = res.data
        if (model.distance > 1000) {
          model.distance = (model.distance / 1000.0).toFixed(1) + 'km'
        } else {
          var num = model.distance;
          model.distance = num.toFixed(1) + 'm';
        }
        var timeString = ''
        if (!util.isNull(model.operatedBegin1)) {
          console.log('转化时间')
          model.operatedBegin1 = util.zhuanHuaTime(model.operatedBegin1, 'h:m')
          model.operatedEnd1 = util.zhuanHuaTime(model.operatedEnd1, 'h:m')
          timeString = model.operatedBegin1 + '-' + model.operatedEnd1
        }
        if (!util.isNull(model.operatedBegin2)) {
          model.operatedBegin2 = util.zhuanHuaTime(model.operatedBegin2, 'h:m')
          model.operatedEnd2 = util.zhuanHuaTime(model.operatedEnd2, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin2 + '-' + model.operatedEnd2

        }
        if (!util.isNull(model.operatedBegin3)) {
          model.operatedBegin3 = util.zhuanHuaTime(model.operatedBegin3, 'h:m')
          model.operatedEnd3 = util.zhuanHuaTime(model.operatedEnd3, 'h:m')
          timeString = timeString + ' ' + model.operatedBegin3 + '-' + model.operatedEnd3
        }
        model.timeString = timeString
        
        if (!util.isBlank(model.shopImg1)) {
          var url1 = config.service.picUrl + model.shopImg1
          model.shopImg1 = url1;
        }
       
        if (model.wdIscountDtoOutList.length > 0) {
          this.setData({
            noTedian: false
          })
        }
        this.setData({
          detailModel: model,
        })
      } else {
        this.show(res.message);
      }
    })
  },
  /**请求订单详情 */
  requestOrderInfo:function(){

    var url = config.service.orderDetailUrl
    var params = {
      id: this.orderId
    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {
      if (res.code == 0) {
        var mo = res.data
        mo.orderTime = util.zhuanHuaTime(mo.orderTime, 'h:m')
        var x = mo.orderDate
        mo.orderDateStr = util.zhuanHuaTime(x, 'Y年M月D日')
        mo.orderDate = util.zhuanHuaTime(x, 'Y-M-D')
        var timeString = ''
        if (!util.isNull(mo.wMerchantDo.operatedBegin1)) {
          mo.wMerchantDo.operatedBegin1 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin1, 'h:m')
          mo.wMerchantDo.operatedEnd1 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd1, 'h:m')
          timeString = mo.wMerchantDo.operatedBegin1 + '-' + mo.wMerchantDo.operatedEnd1
        }
        if (!util.isNull(mo.wMerchantDo.operatedBegin2)) {
          mo.wMerchantDo.operatedBegin2 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin2, 'h:m')
          mo.wMerchantDo.operatedEnd2 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd2, 'h:m')
          timeString = timeString + ' ' + mo.wMerchantDo.operatedBegin2 + '-' + mo.wMerchantDo.operatedEnd2
        }
        if (!util.isNull(mo.wMerchantDo.operatedBegin3)) {
          mo.wMerchantDo.operatedBegin3 = util.zhuanHuaTime(mo.wMerchantDo.operatedBegin3, 'h:m')
          mo.wMerchantDo.operatedEnd3 = util.zhuanHuaTime(mo.wMerchantDo.operatedEnd3, 'h:m')
          timeString = timeString + ' ' + mo.wMerchantDo.operatedBegin3 + '-' + mo.wMerchantDo.operatedEnd3
        }
        mo.wMerchantDo.timeString = timeString;
        
        mo.wMerchantDo.shopImg1 = config.service.picUrl + mo.wMerchantDo.shopImg1

        var countStr = ''
        if (mo.orderNum < 11) {
          countStr = mo.orderNum + '人'
        } else {
          countStr = '10人以上'
        }
        this.setData({
          detailModel:mo.wMerchantDo,
          dateStr: mo.orderDateStr,
          date: mo.orderDate,
          time: mo.orderTime,
          peopleCount: mo.orderNum,
          peopleCountStr: countStr,
          // name: mo.name,
          beizhu:mo.note
        })

        console.log('日期', mo.orderDate);
        if (mo.orderDate != this.currentDay){
          this.setData({
            startTime:'00:00'
          })
        }
        if (!util.isBlank(mo.wdIscountDtoOut)) {
          console.log('有特典');
          var item = mo.wdIscountDtoOut;

          if (!util.isNull(item.limitedAt)) {
            item.limitedAtStr = util.zhuanHuaTime(item.limitedAt, 'Y年M月D日')
          }
          this.setData({
            selectItem: item,
            noTedian: false,
            haveSelect: true,
          })
        }
       
        App.globalData.selectIndex = mo.wdIscountDtoOut
       
      } else {
        this.show(res.message);
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var item = App.globalData.selectIndex
    this.setData({
      haveSelect: false,
      selectItem: {}
    })
    if (!util.isBlank(item)) {
      if (!util.isNull(item.limitedAt) && util.isNull(item.limitedStr)) {
        var arr = teDian.limitedAt.split('-');

        limitedAtStr = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
        item.limitedAtStr = limitedAtStr;

      }
      
      this.setData({
        selectItem: item,
        haveSelect: true,
        noTedian: false
      })
    }

    if(App.userInfo.phone != null){
      this.setData({
        phone: App.userInfo.phone
      })

    } 
  },

  //日期
  changeDate: function (e) {
    var date = e.detail.value
    var arr = date.split('-');
    var dateStr = date
    if (arr.length > 0) {
      dateStr = arr[0] + '年' + arr[1] + '月' + arr[2] + '日';
    }
    if (date != this.currentDay){
      this.setData({
        startTime: '00:00',
      })
    }else{
      this.initStartTime();
      this.setData({
        time:this.currentTime,
        startTime: this.currentTime,
      })
    }
    this.setData({
      date: date,
      dateStr: dateStr,
      
    })
  },
  //请求用户
  requestUserInfo: function () {
    var url = config.service.userInfoUrl;
    var params = {

    }
    net.doRequest(url, params, 'GET', true, net.Token, res => {

      if (res.code == 0) {
        this.setData({
          name: res.data.name,
          phone: res.data.phone

        })
        App.userInfo = res.data;
      } else {
        this.show(res.mesage);
      }
    })
  },

  //时间
  changeTime: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  initStartTime:function(){
    var currentDay = this.getNowDate();
    this.currentDay = currentDay;
    var currentTime = this.getNowTime();
    this.currentTime = currentTime;
    var timeArr = currentTime.split(':')
    if (timeArr[0] == 24) {
      currentTime = '00' + ':' + '0' + timeArr[1];
      if (timeArr[1].length == 2) {
        currentTime = '00' + ':' + timeArr[1];
      }
      this.currentDay = this.getDateStr('', 1);
    
      this.currentTime = currentTime; 
    }
    this.setData({
      startDate: this.currentDay,
      startTime: this.currentTime,
    })
  },

  cellItemClick: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log('当前', index);
    if (index == 0) {
      this.setData({
        showPeopleCount: true,
      })
    }
    if (index == 1) {
      this.setData({

      })
    }
    if (index == 3) {
      wx.navigateTo({
        url: './tedianList?shopId=' + this.shopId + '&select=' + JSON.stringify(this.data.selectItem),
      })
    }
  },
  //选择人数
  peopleCountSelect: function (e) {
    var item = e.currentTarget.dataset.item;
    var countStr = ''
    if (item < 11) {
      countStr = item + '人'
      this.setData({
        peopleCount: item,
        peopleCountStr: countStr,
        showPeopleCount: false,
      });
    } else {
      //10人以上弹出输入框
      this.setData({
        showPeopleCount:false,
        showInputBox:true,
        cc:'',
      })
    }
  
  },

  //确定输入的人数
  inputSureClick:function(){
    if (this.personCount == -1) {
      return;
    }

    if (this.personCount == 0) {
      return;
    }

    this.setData({
      peopleCount: this.personCount,
      peopleCountStr: this.personCount + '人',
      showInputBox: false,
    });
  },

  //人数输入框
  peopleCountInputChange:function(e){
    console.log('输入', e.detail.value);
    if (e.detail.value.length == 0) {
      this.personCount = -1;
    } else {
      this.personCount = e.detail.value;
    }
  },

  submitClick: function (e) {
    var formId = e.detail.formId;
    console.log('提交的Id2', e, formId);
    App.globalData.formId_2 = formId
    if (e.detail.value.nameText.length < 2) {
      this.show('名前を変更ルール (2-10个字)');
      return;
    }

    if (this.data.phone == null) {
      this.show('请输入联系方式');
      return;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(16[0-9]{1}))+\d{8})$/;

    if (!myreg.test(this.data.phone)) {
      this.show('请设置手机号')
      return false;
    }
    
    App.globalData.orderUserName = e.detail.value.nameText;
    App.globalData.orderUserPhone = this.data.phone;
    App.globalData.orderBeizhu = e.detail.value.beizhuText;

    var model2 = {
      peopleCount: this.data.peopleCount,
      peopleCountStr: this.data.peopleCountStr,
      time: this.data.time,
      date: this.data.date
    }
    wx.navigateTo({
      url: './sureAgain?shopId=' + this.shopId + '&model2=' + JSON.stringify(model2) + '&orderId=' + this.orderId,
    })
  },
  modeTap: function () {
    console.log('消失');
    this.setData({
      showPeopleCount: false,
 
    })
  },

  /**获取当天时间 */
  getNowDate: function () {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    //  如果需要时分秒，就放开
    // var h = now.getHours();
    // var m = now.getMinutes();
    // var s = now.getSeconds();
    var formatDate = year + '-' + month + '-' + day;
    return formatDate;
  },

  getNowTime: function () {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth();
    var day = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    if (day < 10) {
      day = '0' + day;
    };
    //  如果需要时分秒，就放开
    var h = now.getHours()+1;
    var m = now.getMinutes();
    var s = now.getSeconds();
    if (h < 10) {
      h = '0' + h;
    };
    if (m < 10) {
      m = '0' + m;
    };
    var formatDate = h + ':' + m
    return formatDate;
  },

  getDateStr: function (today, addDayCount){
    var dd;
    if (today) {
      dd = new Date(today);
    } else {
      dd = new Date();
    }
    dd.setDate(dd.getDate() + addDayCount);//获取AddDayCount天后的日期 
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期 
    var d = dd.getDate();
    if (m < 10) {
      m = '0' + m;
    };
    if (d < 10) {
      d = '0' + d;
    };
    return y + "-" + m + "-" + d;

  },

  //去绑定手机号
  toEditPhone:function(){

    if(this.data.phone != null){
      return;
    }
    wx.navigateTo({
      url: '../mine/edit_detail?isPhone=' + true
    })
  }



  


})

